<template>
  <div>
    <el-input
      v-model="sname"
      placeholder="姓名"
      width="10"
      style="width: 140px"
    ></el-input>
    <el-input
      type="number"
      v-model="sage"
      placeholder="年龄"
      width="10"
      style="width: 140px"
    ></el-input>
    &nbsp;
    <el-button type="primary" @click="findAllManager2">查询</el-button>

    <el-button type="success" @click="dialogVisible = true">添加</el-button>
    <!-- 添加 -->
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-input
            placeholder="姓名"
            v-model="nname"
          ></el-input> </el-form-item
        ><br />
        <el-form-item label="年龄">
          <el-input type="number" placeholder="年龄" v-model="nage"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="clearInfo">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <el-table
      :data="tableData"
      highlight-current-row
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="id" width="40"></el-table-column>
      <el-table-column prop="name" label="姓名" width="150"></el-table-column>

      <el-table-column prop="age" label="年龄" width="100"></el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="deleteUser(scope.row.id)" type="text" size="small"
            >删除</el-button
          >
          <!-- 修改 -->
          <el-dialog
            title="修改信息"
            :visible.sync="dialogVisible2"
            width="30%"
            :before-close="handleClose"
          >
            <el-form :inline="true" class="demo-form-inline">
              <el-form-item label="姓名">
                <el-input
                  placeholder="姓名"
                  v-model="nname"
                ></el-input> </el-form-item
              ><br />
              <el-form-item label="年龄">
                <el-input
                  type="number"
                  placeholder="年龄"
                  v-model="nage"
                ></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="clearInfo">取 消</el-button>
              <el-button type="primary" @click="sendChange()"
                >确 定</el-button
              >
            </span>
          </el-dialog>

          <el-button
            type="text"
            size="small"
            @click="openChange(scope.row.name, scope.row.age,scope.row.id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pageinfo
      :num="pageinit.total"
      @changeSize="changeSize"
      @changeCurrent="changeCurrent"
    ></pageinfo>
  </div>
</template>

<script>
import axios from "axios";
import pageinfo from "@/components/system/pageinfo.vue";
export default {
  data() {
    return {
      //表单显示的数据
      tableData: [],
      //分页数据 初始化的时候
      pageinit: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      name: "",
      age: "",
      nname: "",
      nage: "",
      sname:"",
      sage:"",
      smode:false,
      dialogVisible: false,
      dialogVisible2: false,
      newUser: {
        id: "",
        name: "",
        age: 0,
      },
    };
  },
  //钩子函数 在组件第一次渲染的时候才执行
  created() {
    this.findAllManager();
  },
  components: { pageinfo },
  methods: {
    handleClick(row) {
      console.log(row);
    },
    findAllManager() {
      axios
        .get(
          "/user/" + this.pageinit.currentPage + "/" + this.pageinit.pageSize
        )
        .then((res) => {
          this.tableData = res.data;
          this.pageinit.total=res.data.length;
          // if (res.data.code == 200) {
          //   this.tableData = res.data;
          //   //对综总数量进行赋值
          //   this.pageinit.total = res.data.data.total
          // }
        })
        .catch(() => {});
    },
    //子传父的方法值
    changeSize(val) {
      this.pageinit.pageSize = val;
      this.pageinit.currentPage = 1;
      // 重新调用获取数据方法
      if(this.smode==false){
this.findAllManager();
      }else{
          this.findAllManager2();
      }
      
    },
    changeCurrent(val) {
      this.pageinit.currentPage = val;
      // 重新调用获取数据方法
      if(this.smode==false){
this.findAllManager();
      }else{
          this.findAllManager2();
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    addUser() {
      this.newUser.name = this.nname;
      this.newUser.age = this.nage;
      this.dialogVisible = false;
      axios
        .post("/user/", this.newUser)
        .then(() => {
          this.findAllManager();
          this.clearInfo();
          // if (res.data.code == 200) {
          //   this.tableData = res.data;
          //   //对综总数量进行赋值
          //   this.pageinit.total = res.data.data.total
          // }
        })
        .catch(() => {});
    },
    clearInfo() {
      this.dialogVisible = false;
      this.dialogVisible2 = false;
      this.nname = "";
      this.nage = "";
      this.newUser={}
    },
    deleteUser(uid) {
      axios
        .get("/user/" + uid)
        .then(() => {
          this.findAllManager();
          // if (res.data.code == 200) {
          //   this.tableData = res.data;
          //   //对综总数量进行赋值
          //   this.pageinit.total = res.data.data.total
          // }
        })
        .catch(() => {});
    },
    openChange(name, age,id) {
      this.nname = name;
      this.nage = age;
      this.dialogVisible2 = true;
      this.newUser.id=id;
    },
    sendChange() {
      this.newUser.name = this.nname;
      this.newUser.age = this.nage;
      
      
      this.dialogVisible2=false;
      axios
        .post("/user/change", this.newUser)
        .then(() => {
          this.findAllManager();

          // if (res.data.code == 200) {
          //   this.tableData = res.data;
          //   //对综总数量进行赋值
          //   this.pageinit.total = res.data.data.total
          // }
        })
        .catch(() => {});
    },
    findAllManager2() {
      this.smode=true;
      axios
        .get(
          "/user/" + this.pageinit.currentPage + "/" + this.pageinit.pageSize+ "/"+this.sname+ "/"+this.sage
        )
        .then((res) => {
          this.tableData = res.data;
          this.pageinit.total=res.data.length;
          // if (res.data.code == 200) {
          //   this.tableData = res.data;
          //   //对综总数量进行赋值
          //   this.pageinit.total = res.data.data.total
          // }
        })
        .catch(() => {});
    }
  },

  watch: {
    $route(to, from) {
      console.log(to);
      console.log(from);
    },
  },
};
</script>